#include "Room.hh"
#include <iostream>
#include <algorithm>
#include "Player.hh" 

Room::Room(const std::string& n, const std::string& desc, RoomType type)
    : name(n), description(desc), roomType(type) {
}
Room::~Room() {
    // Clean up the room
    for (auto item : items) {
        delete item;
    }
    items.clear();

    // Clean up the room for enemies
    for (auto enemy : enemies) {
        delete enemy;
    }
    enemies.clear();

    // Note: Connected rooms are not deleted as they may be referenced by other rooms
}

std::string Room::getName() const {
    return name;
}

std::string Room::getDescription() const {
    return description;
}

void Room::addItem(Item* item) {
    items.push_back(item);
}

Item* Room::removeItem(size_t index) {
    if (index < items.size()) {
        Item* item = items[index];
        items.erase(items.begin() + index);
        return item;
    }
    return nullptr;
}

const std::vector<Item*>& Room::getItems() const {
    return items;
}

void Room::addEnemy(Enemy* enemy) {
    enemies.push_back(enemy);
}

void Room::removeEnemy(Enemy* enemy) {
    auto it = std::find(enemies.begin(), enemies.end(), enemy);
    if (it != enemies.end()) {
        enemies.erase(it);
    }
}

const std::vector<Enemy*>& Room::getEnemies() const {
    return enemies;
}

void Room::addExit(const std::string& direction, Room* room) {
    exits[direction] = room;
}

Room* Room::getExit(const std::string& direction) const {
    auto it = exits.find(direction);
    if (it != exits.end()) {
        return it->second;
    }
    return nullptr;
}

const std::map<std::string, Room*>& Room::getExits() const {
    return exits;
}

void Room::display() const {
    std::cout << "=== " << name << " ===" << std::endl;
    std::cout << description << std::endl << std::endl;

    // Displays the items in the room
    if (!items.empty()) {
        std::cout << "Items in the room: ";
        for (size_t i = 0; i < items.size(); ++i) {
            std::cout << items[i]->toString();
            if (i < items.size() - 1) {
                std::cout << ", ";
            }
        }
        std::cout << std::endl;
    }
    else {
        std::cout << "There were no items in the room." << std::endl;
    }

    // Displays enemies in the room
    if (!enemies.empty()) {
        std::cout << "Enemies in the room: ";
        for (size_t i = 0; i < enemies.size(); ++i) {
            std::cout << enemies[i]->toString();
            if (i < enemies.size() - 1) {
                std::cout << ", ";
            }
        }
        std::cout << std::endl;
    }
    else {
        std::cout << "There are no enemies in the room." << std::endl;
    }

    // Displays the available exports
    if (!exits.empty()) {
        std::cout << "Available exports: ";
        size_t i = 0;
        for (const auto& exit : exits) {
            std::cout << exit.first;
            if (i < exits.size() - 1) {
                std::cout << ", ";
            }
            ++i;
        }
        std::cout << std::endl;
    }
    else {
        std::cout << "There is no exit to this room." << std::endl;
    }

    std::cout << std::endl;
}


void Room::applyRoomEffect(Player& player) {
    switch (roomType) {
    case RoomType::HEALING: {
        unsigned healAmount = player.getMaxHealth() / 2;
        player.takeDamage(healAmount); // A negative number indicates a treatment
        std::cout << "The magic of the healing chamber restores you" << healAmount << "HP!\n";
        break;
    }
    case RoomType::DAMAGE: {
        unsigned damage = 5 + (rand() % 6); // The toxic gases of the gas chambers have caused you
        player.takeDamage(damage);
        std::cout << "The toxic gases of the gas chambers have caused you" << damage << "point damage!\n";
        break;
    }
    case RoomType::TRAP: {
        if (rand() % 100 < 30) { // 30%chance to trigger a trap
            unsigned damage = 3 + (rand() % 4); // 3-6 damage
            player.takeDamage(damage);
            std::cout << "You triggered the trap, received" << damage << "point damage!\n";
        }
        break;
    }
    default:
        break;
    }
}


//这段代码实现了一个游戏中的房间类 Room，负责管理房间内的物品、敌人、出口以及特殊房间效果。
//特殊的房间：
// 1.治疗室（HEALING）：恢复玩家最大生命值的 50 % （例如最大生命 20 → 恢复 10 点）。
// 2.毒气室（DAMAGE）：对玩家造成 5 - 10 点随机伤害（无视防御）。
// 3.陷阱室（TRAP）：30 % 概率触发陷阱，造成 3 - 6 点随机伤害。


//This code implements an in-game room class that manages items, enemies, exits, and special room effects in the room.
//Special Rooms :
//1. HEALING : Restore 50 % of the player's maximum health (e.g. 20 max health→ recover 10 points).
//2. DAMAGE : Deal 5 - 10 random damage to the player(ignoring defense).
//3. TRAP : 30 % chance to trigger a trap that deals 3 - 6 random damage.